﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class CreateNewStadium : System.Web.UI.Page
    {
        string StadiumId = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.CreateNewStadium_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            //string StadiumId = null;
            StadiumId = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetStadiumByUser", ou.UserId));
            if (StadiumId != "Null")
                Response.Redirect("~/StadiumPage.aspx?StadiumId=" + StadiumId);

            this.StadiumNameTextBox.ReadOnly = false;
            this.BasicSeatsTextBox.ReadOnly = false;
            this.SeatsUnderRoofTextBox.ReadOnly = false;
            this.SeatsInVIPTextBox.ReadOnly = false;
            this.CalculateNewStadiumCostButton.Visible = true;
            this.BuildButton.Visible = false;
            this.ChangeSeatsValuesHyperLink.Visible = false;

            if (!IsPostBack) //fired only the first time the page is loading
            {
                if (Request.QueryString["StadiumName"] != null)
                    this.StadiumNameTextBox.Text = Request.QueryString["StadiumName"].ToString();

                if (Request.QueryString["BasicSeats"] != null)
                    this.BasicSeatsTextBox.Text = Request.QueryString["BasicSeats"].ToString();

                if (Request.QueryString["UnderRoof"] != null)
                    this.SeatsUnderRoofTextBox.Text = Request.QueryString["UnderRoof"].ToString();

                if (Request.QueryString["Vip"] != null)
                    this.SeatsInVIPTextBox.Text = Request.QueryString["Vip"].ToString();

                if (!GND.ReadNivdalBoolData("UserId", "Nivdal_IsUserThereForMonth", ou.UserId))
                    this.MessageLabel.Text = Resources.Resource.CreateNewStadium_Warning;

                bool IsNewTeam = GND.ReadNivdalBoolData("TeamId", "Nivdal_StadiumIsTeamNewForStadium", ou.TeamId);
               
                if (IsNewTeam)
                    OldStartStadiumPanel.Visible = false;
                else
                    NewStartstadiumPanel.Visible = false;

                // deal with niewbie tips
                if (!IsPostBack)
                {
                    if ((int)Session["NewbieTipStage"] == 5)
                    {
                        Session["NewbieTipStage"] = 6;
                        Session["NewbieTipText"] = GND.ReadNivdalStringData("TipId", "Language", "dbo.Nivdal_NewbieTipsGetTip", "6", ou.Language);
                        GND.ExecNivdalSQL("UserId", "StageId", "Nivdal_NewbieTipsSetStage", ou.UserId, "6");
                    }
                }

            }
        }

        protected void CalculateNewStadiumCostButton_Click(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            this.MessageLabel.ForeColor = System.Drawing.Color.Green;
            decimal MaxValue = 100000;
            bool IsInputOK = true;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            string StadiumName = this.StadiumNameTextBox.Text.Trim();
            string BasicSeats = this.BasicSeatsTextBox.Text.Trim();
            string UnderRoof = this.SeatsUnderRoofTextBox.Text.Trim();
            string Vip = this.SeatsInVIPTextBox.Text.Trim();
            if (StadiumName.Length == 0 || BasicSeats.Length == 0 || UnderRoof.Length == 0 || Vip.Length == 0)
            {
                this.MessageLabel.ForeColor = System.Drawing.Color.Red;
                this.MessageLabel.Text = Resources.Resource.CreateNewStadium_ValueErrorLeft + " " + MaxValue + " " + Resources.Resource.CreateNewStadium_ValueErrorRight;
                IsInputOK = false;
            }
            else if ((Convert.ToDecimal(BasicSeats) > MaxValue) || (Convert.ToDecimal(UnderRoof) > MaxValue) || (Convert.ToDecimal(Vip) > MaxValue))
            {
                this.MessageLabel.ForeColor = System.Drawing.Color.Red;
                this.MessageLabel.Text = Resources.Resource.CreateNewStadium_MaxValueBelow + " " + MaxValue;
                IsInputOK = false;
            }
            if (IsInputOK)
            {
                this.StadiumNameTextBox.ReadOnly = true;
                this.BasicSeatsTextBox.ReadOnly = true;
                this.SeatsUnderRoofTextBox.ReadOnly = true;
                this.SeatsInVIPTextBox.ReadOnly = true;
                bool UsedDiscount = GND.ReadNivdalBoolData("TeamId", "Nivdal_StadiumDidTeamUseConstructionDiscount", ou.TeamId);
                this.ShowConstructionCostLabel.Text = (GND.ReadNivdalStringData("@Basic", "@UnderRoof", "@Vip", "Language", "UsedDiscount", "Nivdal_StadiumShowConstructionCost", BasicSeats, UnderRoof, Vip, ou.Language, UsedDiscount));
                this.CalculateNewStadiumCostButton.Visible = false;
                this.BuildButton.Visible = true;
                this.ChangeSeatsValuesHyperLink.Visible = true;
                //this.ChangeSeatsValuesHyperLink.NavigateUrl = ("~/CreateNewStadium.aspx");
                this.ChangeSeatsValuesHyperLink.PostBackUrl = ("~/CreateNewStadium.aspx?StadiumName=" + StadiumName + "&BasicSeats=" + BasicSeats + "&UnderRoof=" + UnderRoof + "&Vip=" + Vip);
            }
        }
        protected void BuildButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string StadiumName = this.StadiumNameTextBox.Text.Trim();
            string BasicSeats = this.BasicSeatsTextBox.Text.Trim();
            string UnderRoof = this.SeatsUnderRoofTextBox.Text.Trim();
            string Vip = this.SeatsInVIPTextBox.Text.Trim();
            
            this.BuildButton.Visible = false;
            this.ChangeSeatsValuesHyperLink.Visible = false;

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_StadiumCreateNewStadium";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = ou.UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_StadiumName = new SqlParameter();
            dbParam_StadiumName.ParameterName = "@StadiumName";
            dbParam_StadiumName.Value = StadiumName;
            dbParam_StadiumName.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_StadiumName);

            SqlParameter dbParam_Basic = new SqlParameter();
            dbParam_Basic.ParameterName = "@Basic";
            dbParam_Basic.Value = BasicSeats;
            dbParam_Basic.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Basic);

            SqlParameter dbParam_UnderRoof = new SqlParameter();
            dbParam_UnderRoof.ParameterName = "@UnderRoof";
            dbParam_UnderRoof.Value = UnderRoof;
            dbParam_UnderRoof.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UnderRoof);

            SqlParameter dbParam_Vip = new SqlParameter();
            dbParam_Vip.ParameterName = "@VIP";
            dbParam_Vip.Value = Vip;
            dbParam_Vip.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Vip);

            SqlParameter dbParam_Message = new SqlParameter();
            dbParam_Message.ParameterName = "@Message";
            dbParam_Message.Value = "";
            dbParam_Message.DbType = DbType.String;
            dbParam_Message.Direction = ParameterDirection.Output;
            dbParam_Message.Size = 64;
            dbCommand.Parameters.Add(dbParam_Message);


            SqlParameter dbParam_StadiumId = new SqlParameter();
            dbParam_StadiumId.ParameterName = "@StadiumId";
            dbParam_StadiumId.Value = 0;
            dbParam_StadiumId.DbType = DbType.Decimal;
            dbParam_StadiumId.Direction = ParameterDirection.Output;
            dbCommand.Parameters.Add(dbParam_StadiumId);


            try
            {
                dbConnection.Open();
                dbCommand.ExecuteScalar();
                this.ShowConstructionCostLabel.Text = dbParam_Message.Value.ToString();
                if (dbParam_StadiumId.Value.ToString() == "0")
                {
                    this.ChangeSeatsValuesHyperLink.Visible = true;
                    this.ChangeSeatsValuesHyperLink.PostBackUrl = ("~/CreateNewStadium.aspx?StadiumName=" + StadiumName + "&BasicSeats=" + BasicSeats + "&UnderRoof=" + UnderRoof + "&Vip=" + Vip);
                }
                else
                {
                    Response.Redirect("~/StadiumPage.aspx?StadiumId=" + dbParam_StadiumId.Value.ToString());
                }
            }


            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
                return;
            }

            finally
            {
                dbConnection.Close();
            }
        }
        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string BuildButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.CreateNewStadium_BuildButtonOnClientClick + "')");
        }

        protected void NewCreateNewstadium(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            GND.ExecNivdalSQL("UserId","StadiumName", "Nivdal_StadiumCreateDiscount5KStadium", ou.UserId, NewStadiumNameTextBox.Text.Trim());

            Response.Redirect("~/StadiumPage.aspx");
        }


    }
}